package bairui.international.console.web.controller.finance.score;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.console.web.controller.order.product.C_ProductOrder_Detail_ShowOrderProduct;
import bairui.international.model.domain.jpa.QTB_ProductOrderItem;
import bairui.international.model.domain.jpa.TB_ProductOrderItem;
import bairui.international.service.Service_ProductOrderItem;

/**
 *
 * 详情
 *
 * @generated
 * 
 * @see STest_C_Finance_ProductScoreOrder_Detail_ProductPage 测试
 *
 */
@Controller
public class C_Finance_ProductScoreOrder_Detail_ProductPage
 extends SC_Finance_ProductScoreOrder_Detail_ProductPage 
{
	
	@Autowired private Service_ProductOrderItem service_ProductOrderItem;
	
	/**
	 * 订单明细
	 */
	@Action(ProductPage.class)
	@GetMapping(value=ProductPage.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object productPage(
		HttpServletRequest request, 
		@PathVariable(name=ProductPage.PathVariableName.Id) long id
	) throws Exception {
		org.springframework.data.domain.Page<TB_ProductOrderItem> page = service_ProductOrderItem.page(
				it->filter(it, id, request), 
				ProductPage.Output.page(request), 
				it->sort(it, request)
			);
		//
		page.forEach(it->{
			it.setProductImgUrl(new C_ProductOrder_Detail_ShowOrderProduct.ShowOrderProduct.Operate() {
				@Override public Object getId() {
					return it.getId();
				}
			}.getUrl());
		});
		//
		return ProductPage.Output.output(
				page
			);
	}
	
	/*
	 * 过滤
	 */
	static BooleanBuilder filter(QTB_ProductOrderItem it, long id, HttpServletRequest request){
		return ProductPage.Filter.query()
				.and(it.productOrder.id.eq(id));
	}
	
	/*
	 * 排序
	 */
	static Sort sort(QTB_ProductOrderItem it, HttpServletRequest request){
		return ProductPage.Output.sort(it, request, new ProductPage.Output.OrderExtends(){
				@Override public Sort gainId(Direction direction) {
					return sort(direction, it.id.product.id);
				}
			});
	}
	
	

}
